package com.td.module.system.service.video;

import com.td.framework.common.pojo.PageResult;
import com.td.module.system.controller.admin.video.vo.video.VideoCreateReqVO;
import com.td.module.system.controller.admin.video.vo.video.VideoExportReqVO;
import com.td.module.system.controller.admin.video.vo.video.VideoPageReqVO;
import com.td.module.system.controller.admin.video.vo.video.VideoUpdateReqVO;
import com.td.module.system.convert.video.VideoConvert;
import com.td.module.system.dal.dataobject.video.VideoDO;
import com.td.module.system.dal.mysql.video.VideoMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.td.module.system.enums.ErrorCodeConstants.VIDEO_NOT_EXISTS;

/**
 * 萤石账号管理 Service 实现类
 *
 * @author 王翔翔
 */
@Service
@Validated
public class VideoServiceImpl implements VideoService {

    @Resource
    private VideoMapper videoMapper;

    @Override
    public Long createVideo(VideoCreateReqVO createReqVO) {
        // 插入
        VideoDO video = VideoConvert.INSTANCE.convert(createReqVO);
        videoMapper.insert(video);
        // 返回
        return video.getId();
    }

    @Override
    public void updateVideo(VideoUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateVideoExists(updateReqVO.getId());
        // 更新
        VideoDO updateObj = VideoConvert.INSTANCE.convert(updateReqVO);
        videoMapper.updateById(updateObj);
    }

    @Override
    public void deleteVideo(Long id) {
        // 校验存在
        this.validateVideoExists(id);
        // 删除
        videoMapper.deleteById(id);
    }

    private void validateVideoExists(Long id) {
        if (videoMapper.selectById(id) == null) {
            throw exception(VIDEO_NOT_EXISTS);
        }
    }

    @Override
    public VideoDO getVideo(Long id) {
        return videoMapper.selectById(id);
    }

    @Override
    public List<VideoDO> getVideoList(Collection<Long> ids) {
        return videoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<VideoDO> getVideoPage(VideoPageReqVO pageReqVO) {
        return videoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<VideoDO> getVideoList(VideoExportReqVO exportReqVO) {
        return videoMapper.selectList(exportReqVO);
    }

    @Override
    public VideoDO getToken(String appKey) {
        return videoMapper.selectByAppKey(appKey);
    }

}
